
<html><head>
<script type="text/javascript">

setPublic(AsyncModel, "com.cordys.opensource.crom");

importType("cas.xforms.runtimelibrary.CompositeControl");
inherit(AsyncModel, CompositeControl);

function AsyncModel()
{
}

AsyncModel.attachType = function ( control )
{
	// Start listening on soap requests...
	system.addListener("onrequest", systemRequestListener, control);
	// ... and soap responses.
	system.addListener("onresponse", systemResponseListener, control);
}

// This function listens to the "onrequest" event of the system object.
function systemRequestListener(eventObject, control)
{
	var xformsModel = getXFormsModel(control);
   	if (!xformsModel) // There is no XForms model??
	{
		return;
	}

	// Now check whether the request comes from the XForms model.
	if (eventObject.soapTransaction.srcElement!=xformsModel.dataisland.__busDataIsland)
	{
		// Request comes from a different place.
		return;
	}

	
	var async = true;
	var asyncAttribute = control.getAttribute("async");
	if (typeof(asyncAttribute)=="string")
	{
		async = asyncAttribute.toLowerCase()!="false";
	}

	eventObject.soapTransaction.async = async;
}

// This function listens to the "onrequest" event of the system object.
function systemResponseListener(eventObject, control)
{
	var xformsModel = getXFormsModel(control);
   	if (!xformsModel) // There is no XForms model??
	{
		return;
	}

	// Now check whether the response belongs to the XForms model.
	if (eventObject.soapTransaction.srcElement!=xformsModel.dataisland.__busDataIsland)
	{
		// Response comes for a different thing.
		return;
	}
	
	if (eventObject.soapTransaction.async==false)
	{
		// This means it is still a synchronous request. Let's leave it up to XForms to handle the response.
		return;
	}
	
	// Now grab the response and give it back to the model.
	var response = eventObject.soapTransaction.xmlhttpObject.responseXML;
	xformsModel.putData(response, true);
}

function getXFormsModel(control)
{
	var modelName = control.getAttribute("model");
	if (!modelName)
	{
		// Hmmm... this is kind of strange. Apparently the model attribute has been removed from our composite control?
		return;
	}
	return control.ownerDocument.defaultView[modelName];
}

AsyncModel.detachType = function ( control )
{
	// Stop listening on soap requests...
	system.removeListener("onrequest", systemRequestListener, control);
	// ... and soap responses.
	system.removeListener("onresponse", systemResponseListener, control);
}
</script>
</head><body></body></html>